/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handlers
 * FILE       : QuestionInfo.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 16, 2010  mycodetalks			       Created
 */
package net.lwf.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.lwf.common.constant.ICommonConstant;
import net.lwf.number.util.NumberFormatter;
import net.lwf.number.util.RandomNumberGenerator;

import lwf.handler.entities.RadioData;

/**
 * Class description goes here
 */
public class QuestionInfo extends AttemptedQuestion {
	
	private static final long serialVersionUID = 1L;
	
	private Object questionObject; //will be removed later
	private int questionNumber = 1;
	private String operation;
	private int decimalDigits;
	private String htmlQuestion;
	private List<RadioData> radioAnswerChoices;
	private Object placeHolder;
	//private boolean ignoreRadioChoices;
	
	/**
	 * Use this constructor when generating the answer choices for numbers and
	 * decimals.
	 */
	public QuestionInfo() {
	}
	
	public QuestionInfo(List<RadioData> answers) {
		this.radioAnswerChoices = answers;
	}
	
	public Object getQuestionObject() { 
		return questionObject;
	}
	public void setQuestionObject(Object questionObject) {
		this.questionObject = questionObject;
	}

	public int getQuestionNumber() {
		return questionNumber;
	}
	public void setQuestionNumber(int questionNumber) {
		this.questionNumber = questionNumber;
	}
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	public int getDecimalDigits() {
		return decimalDigits;
	}
	public void setDecimalDigits(int decimalDigits) {
		this.decimalDigits = decimalDigits;
	}
	public String getHtmlQuestion() {
		return htmlQuestion;
	}
	public void setHtmlQuestion(String htmlQuestion) {
		this.htmlQuestion = htmlQuestion;
	}
	public List<RadioData> getRadioAnswerChoices() {
		if(this.radioAnswerChoices == null) {
			this.generateRadioAnswerChoises();
		}
		return this.radioAnswerChoices;
	}
	public void setRadioAnswerChoices(List<RadioData> radioAnswerChoices) {
		this.radioAnswerChoices = radioAnswerChoices;
	}
	
	public Object getPlaceHolder() {
		return placeHolder;
	}

	public void setPlaceHolder(Object placeHolder) {
		this.placeHolder = placeHolder;
	}

	private void generateRadioAnswerChoises() {
		
		List<RadioData> rData = new ArrayList<RadioData>();
		String formattedAnswer = NumberFormatter.format(getAnswer(), getDecimalDigits());
		//the answer is a formatted, May contain comma (s).
		//So remove them
		String answer = StringUtils.remove(formattedAnswer, ICommonConstant.COMMA);
		
		rData.add(new RadioData(formattedAnswer, formattedAnswer));
		String formattedvalue = "";
		RadioData data = null;
		if (this.decimalDigits == 0) {
			List<Integer> answerChoices = RandomNumberGenerator.generateInts(formattedAnswer.trim().length(), 3);
			for (Integer intger : answerChoices) {
				String value = String.valueOf(intger.intValue());
				formattedvalue = NumberFormatter.format(value, getDecimalDigits());
				if(!value.equals(answer)) {
					data = new RadioData(formattedvalue, formattedvalue);
					rData.add(data);
				}
			}
		}
		else {
			double base = Double.parseDouble(answer);
			List<Double> dChoices = RandomNumberGenerator.generateDouble(base, 25, 3);
			for (Double doubleval: dChoices) {
				formattedvalue = NumberFormatter.format(doubleval.toString(), getDecimalDigits());
				data = new RadioData(formattedvalue, formattedvalue);
				rData.add(data);
			}
		}

		Collections.shuffle(rData);
		
		this.radioAnswerChoices = rData;
	}

} //QuestionInfo
/*public class QuestionInfo {
	
	private Object questionObject;
	private String question;
	private String answer;
	private int questionNumber = 1;
	
	public Object getQuestionObject() {
		return questionObject;
	}
	public void setQuestionObject(Object questionObject) {
		this.questionObject = questionObject;
	}
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public int getQuestionNumber() {
		return questionNumber;
	}
	public void setQuestionNumber(int questionNumber) {
		this.questionNumber = questionNumber;
	}

} //QuestionInfo
*/


